Boilerplate¶

In [1]:
import bayesian_multitarget_latent_factors as bmlf
import numpy as np
import matplotlib.pyplot as plt
In [2]:
def plot_all_B_dict(B_dict, ax = None):
    if ax is None:
        fig, ax = plt.subplots(1,1,figsize=(13,8))
    else:
        fig = ax.get_figure()
    for i in range(B_dict['p']):
        bmlf.plot_col_basis_dict(B_dict, col_idx=i, ax=ax)
    ax.get_legend().set_visible(False)
    ax.set_title('')
    return fig, ax

Basis Functions¶

Equispaced 1D b-spline¶

In [3]:
B = bmlf.make_basis_dict_structured(
    basis_type='bspline',
    dimensionality=1,
    p=3,
    n_basis=10,
    domain_range=(0, 1),
    num_points=100,
    add_optional_terms='linear',
)
plot_all_B_dict(B)
Out[3]:
(<Figure size 1300x800 with 1 Axes>, <Axes: xlabel='X Axis', ylabel='Value'>)
No description has been provided for this image
In [4]:
fig, axs = plt.subplots(1,2, figsize=(13,5))
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[0])
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[1], corr=True)
No description has been provided for this image

Unstructured 1D b-spline¶

In [5]:
x = np.power(10,np.linspace(-3,0,50))
B = bmlf.make_basis_dict_unstructured(
    x,
    basis_type='bspline',
    dimensionality=1,
    p=3,
    n_basis=10,
    domain_range=(0, 1),
    add_optional_terms='linear',
)
plot_all_B_dict(B)
Out[5]:
(<Figure size 1300x800 with 1 Axes>, <Axes: xlabel='X Axis', ylabel='Value'>)
No description has been provided for this image
In [6]:
fig, axs = plt.subplots(1,2, figsize=(13,5))
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[0])
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[1], corr=True)
No description has been provided for this image

Equispaced 1D Fourier¶

In [7]:
B = bmlf.make_basis_dict_structured(
    basis_type='fourier',
    dimensionality=1,
    n_basis=10,
    domain_range=(0, 1),
    num_points=100,
    add_optional_terms='constant',
)
plot_all_B_dict(B)
Out[7]:
(<Figure size 1300x800 with 1 Axes>, <Axes: xlabel='X Axis', ylabel='Value'>)
No description has been provided for this image
In [8]:
fig, axs = plt.subplots(1,2, figsize=(13,5))
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[0])
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[1], corr=True)
No description has been provided for this image

Equispaced 2D Fourier (Tensor Product of Fourier 1D)¶

In [9]:
B = bmlf.make_basis_dict_structured(
    basis_type='fourier',
    dimensionality=2,
    n_basis=10,
    domain_range=(0, 1),
    num_points=100,
    add_optional_terms='constant',
)

fig, axs = plt.subplots(1,3,figsize=(20,6))
bmlf.plot_col_basis_dict(B, col_idx=2, ax=axs[0])
bmlf.plot_col_basis_dict(B, col_idx=21, ax=axs[1])
bmlf.plot_col_basis_dict(B, col_idx=50, ax=axs[2])
No description has been provided for this image
In [10]:
fig, axs = plt.subplots(1,2, figsize=(13,5))
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[0])
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[1], corr=True)
No description has been provided for this image

Equispaced 2D b-splines (Tensor Product of b-splines 1D)¶

In [11]:
B = bmlf.make_basis_dict_structured(
    basis_type='bspline',
    dimensionality=2,
    p=3,
    n_basis=10,
    domain_range=(0, 1),
    num_points=100,
    add_optional_terms='bilinear',
)

fig, axs = plt.subplots(1,3,figsize=(20,6))
bmlf.plot_col_basis_dict(B, col_idx=2, ax=axs[0])
bmlf.plot_col_basis_dict(B, col_idx=35, ax=axs[1])
bmlf.plot_col_basis_dict(B, col_idx=68, ax=axs[2])
No description has been provided for this image
In [12]:
fig, axs = plt.subplots(1,3,figsize=(20,6))
bmlf.plot_col_basis_dict(B, col_idx=101, ax=axs[0])
bmlf.plot_col_basis_dict(B, col_idx=102, ax=axs[1])
bmlf.plot_col_basis_dict(B, col_idx=103, ax=axs[2])
No description has been provided for this image
In [13]:
fig, axs = plt.subplots(1,2, figsize=(13,5))
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[0])
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[1], corr=True)
No description has been provided for this image

Equispaced 1D Radial Bumps¶

In [14]:
B = bmlf.make_basis_dict_structured(
    basis_type='radial_bump',
    dimensionality=1,
    n_basis=10,
    R = 0.2,
    domain_range=(0, 1),
    num_points=100,
    add_optional_terms='constant',
)
plot_all_B_dict(B)
Out[14]:
(<Figure size 1300x800 with 1 Axes>, <Axes: xlabel='X Axis', ylabel='Value'>)
No description has been provided for this image
In [15]:
fig, axs = plt.subplots(1,2, figsize=(13,5))
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[0])
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[1], corr=True)
No description has been provided for this image

Equispaced 2D Radial Bumps¶

In [16]:
B = bmlf.make_basis_dict_structured(
    basis_type='radial_bump',
    dimensionality=2,
    R = 0.3,
    n_basis=10,
    domain_range=(0, 1),
    num_points=100,
    add_optional_terms='constant',
)

fig, axs = plt.subplots(1,3,figsize=(20,6))
bmlf.plot_col_basis_dict(B, col_idx=2, ax=axs[0])
bmlf.plot_col_basis_dict(B, col_idx=21, ax=axs[1])
bmlf.plot_col_basis_dict(B, col_idx=50, ax=axs[2])
No description has been provided for this image
In [17]:
fig, axs = plt.subplots(1,2, figsize=(13,5))
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[0])
bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[1], corr=True)
No description has been provided for this image

Approximation of Gaussian Processes Covariance Structures by means of Spectral Decomposition¶

1D Exponential Quadratic Covariance Kernel with Varying $\rho$¶

In [18]:
rho_list = [0.1,0.2,0.5]
fig, axs = plt.subplots(3,1,figsize=(13,15))

for i in range(len(rho_list)):
    x = np.linspace(0,1,100)
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=10,
        type_cov='exp_quad'
    )
    plot_all_B_dict(B, axs[i])
    axs[i].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$')
No description has been provided for this image
In [19]:
fig, axs = plt.subplots(3,3,figsize=(16,15))

for i in range(len(rho_list)):
    x = np.linspace(0,1,100)
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=10,
        type_cov='exp_quad'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,0])
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,1], corr=True)
    axs[i,0].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Covariance')
    axs[i,1].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Correlation')
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=100,
        type_cov='exp_quad'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,2])
    axs[i,2].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Σ (no approximation)')
No description has been provided for this image
In [20]:
rho_list = [0.1,0.2,0.5]
fig, axs = plt.subplots(3,1,figsize=(13,15))

for i in range(len(rho_list)):
    x = np.linspace(0,1,100)
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=15,
        type_cov='exp_quad'
    )
    U = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=15,
        type_cov='exp_quad',
        use_S=False
    )
    S = np.square( np.diag(U['B'].T@B['B']) )
    axs[i].plot(S, 'ko-')
    axs[i].set_title('Singular Values: ' + r'$\rho = ' + f'{rho_list[i]:.2f}' + '$')
    axs[i].set_ylim(bottom=0.0)
No description has been provided for this image

1D Matern $5/2$ Covariance Kernel with Varying $\rho$¶

In [21]:
rho_list = [0.1,0.2,0.5]
fig, axs = plt.subplots(3,1,figsize=(13,15))

for i in range(len(rho_list)):
    x = np.linspace(0,1,100)
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=10,
        type_cov='matern_5_2'
    )
    plot_all_B_dict(B, axs[i])
    axs[i].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$')
No description has been provided for this image
In [22]:
fig, axs = plt.subplots(3,3,figsize=(16,15))

for i in range(len(rho_list)):
    x = np.linspace(0,1,100)
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=10,
        type_cov='matern_5_2'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,0])
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,1], corr=True)
    axs[i,0].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Covariance')
    axs[i,1].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Correlation')
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=100,
        type_cov='matern_5_2'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,2])
    axs[i,2].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Σ (no approximation)')
No description has been provided for this image
In [23]:
rho_list = [0.1,0.2,0.5]
fig, axs = plt.subplots(3,1,figsize=(13,15))

for i in range(len(rho_list)):
    x = np.linspace(0,1,100)
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=15,
        type_cov='matern_5_2'
    )
    U = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=15,
        type_cov='matern_5_2',
        use_S=False
    )
    S = np.square( np.diag(U['B'].T@B['B']) )
    axs[i].plot(S, 'ko-')
    axs[i].set_title('Singular Values: ' + r'$\rho = ' + f'{rho_list[i]:.2f}' + '$')
    axs[i].set_ylim(bottom=0.0)
No description has been provided for this image

1D Matern $3/2$ Covariance Kernel with Varying $\rho$¶

In [24]:
rho_list = [0.1,0.2,0.5]
fig, axs = plt.subplots(3,1,figsize=(13,15))

for i in range(len(rho_list)):
    x = np.linspace(0,1,100)
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=10,
        type_cov='matern_3_2'
    )
    plot_all_B_dict(B, axs[i])
    axs[i].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$')
No description has been provided for this image
In [25]:
fig, axs = plt.subplots(3,3,figsize=(16,15))

for i in range(len(rho_list)):
    x = np.linspace(0,1,100)
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=10,
        type_cov='matern_3_2'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,0])
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,1], corr=True)
    axs[i,0].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Covariance')
    axs[i,1].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Correlation')
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=100,
        type_cov='matern_3_2'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,2])
    axs[i,2].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Σ (no approximation)')
No description has been provided for this image
In [26]:
rho_list = [0.1,0.2,0.5]
fig, axs = plt.subplots(3,1,figsize=(13,15))

for i in range(len(rho_list)):
    x = np.linspace(0,1,100)
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=15,
        type_cov='matern_3_2'
    )
    U = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=15,
        type_cov='matern_3_2',
        use_S=False
    )
    S = np.square( np.diag(U['B'].T@B['B']) )
    axs[i].plot(S, 'ko-')
    axs[i].set_title('Singular Values: ' + r'$\rho = ' + f'{rho_list[i]:.2f}' + '$')
    axs[i].set_ylim(bottom=0.0)
No description has been provided for this image

2D Exponential Quadratic Covariance Kernel¶

In [27]:
B = bmlf.make_basis_dict_structured(
    basis_type='bspline',
    dimensionality=2,
    p=3,
    n_basis=10,
    domain_range=(0, 1),
    num_points=10,
    add_optional_terms='bilinear',
)
x = B['t']
B = bmlf.build_matrix_B_from_gaussian_process_covariance(
    x,
    0.4,
    num_singular_values=10,
    type_cov='exp_quad'
)


fig, axs = plt.subplots(1,3,figsize=(20,6))
bmlf.plot_col_basis_dict(B, col_idx=0, ax=axs[0])
bmlf.plot_col_basis_dict(B, col_idx=4, ax=axs[1])
bmlf.plot_col_basis_dict(B, col_idx=8, ax=axs[2])
No description has been provided for this image
In [28]:
rho_list = [0.1,0.2,0.5]

fig, axs = plt.subplots(3,3,figsize=(16,15))

for i in range(len(rho_list)):
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=50,
        type_cov='exp_quad'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,0])
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,1], corr=True)
    axs[i,0].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Covariance')
    axs[i,1].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Correlation')
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=100,
        type_cov='exp_quad'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,2])
    axs[i,2].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Σ (no approximation)')
No description has been provided for this image
In [29]:
rho_list = [0.1,0.2,0.5]
fig, axs = plt.subplots(3,1,figsize=(13,15))

for i in range(len(rho_list)):
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=50,
        type_cov='exp_quad'
    )
    U = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=50,
        type_cov='exp_quad',
        use_S=False
    )
    S = np.square( np.diag(U['B'].T@B['B']) )
    axs[i].plot(S, 'ko-')
    axs[i].set_title('Singular Values: ' + r'$\rho = ' + f'{rho_list[i]:.2f}' + '$')
    axs[i].set_ylim(bottom=0.0)
No description has been provided for this image

2D Matern $5/2$ Covariance Kernel¶

In [30]:
B = bmlf.make_basis_dict_structured(
    basis_type='bspline',
    dimensionality=2,
    p=3,
    n_basis=10,
    domain_range=(0, 1),
    num_points=10,
    add_optional_terms='bilinear',
)
x = B['t']
B = bmlf.build_matrix_B_from_gaussian_process_covariance(
    x,
    0.4,
    num_singular_values=10,
    type_cov='matern_5_2'
)


fig, axs = plt.subplots(1,3,figsize=(20,6))
bmlf.plot_col_basis_dict(B, col_idx=0, ax=axs[0])
bmlf.plot_col_basis_dict(B, col_idx=4, ax=axs[1])
bmlf.plot_col_basis_dict(B, col_idx=8, ax=axs[2])
No description has been provided for this image
In [31]:
rho_list = [0.1,0.2,0.5]

fig, axs = plt.subplots(3,3,figsize=(16,15))

for i in range(len(rho_list)):
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=50,
        type_cov='matern_5_2'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,0])
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,1], corr=True)
    axs[i,0].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Covariance')
    axs[i,1].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Correlation')
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=100,
        type_cov='matern_5_2'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,2])
    axs[i,2].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Σ (no approximation)')
No description has been provided for this image
In [32]:
rho_list = [0.1,0.2,0.5]
fig, axs = plt.subplots(3,1,figsize=(13,15))

for i in range(len(rho_list)):
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=50,
        type_cov='matern_5_2'
    )
    U = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=50,
        type_cov='matern_5_2',
        use_S=False
    )
    S = np.square( np.diag(U['B'].T@B['B']) )
    axs[i].plot(S, 'ko-')
    axs[i].set_title('Singular Values: ' + r'$\rho = ' + f'{rho_list[i]:.2f}' + '$')
    axs[i].set_ylim(bottom=0.0)
No description has been provided for this image

2D Matern $3/2$ Covariance Kernel¶

In [33]:
B = bmlf.make_basis_dict_structured(
    basis_type='bspline',
    dimensionality=2,
    p=3,
    n_basis=10,
    domain_range=(0, 1),
    num_points=10,
    add_optional_terms='bilinear',
)
x = B['t']
B = bmlf.build_matrix_B_from_gaussian_process_covariance(
    x,
    0.4,
    num_singular_values=10,
    type_cov='matern_3_2'
)


fig, axs = plt.subplots(1,3,figsize=(20,6))
bmlf.plot_col_basis_dict(B, col_idx=0, ax=axs[0])
bmlf.plot_col_basis_dict(B, col_idx=4, ax=axs[1])
bmlf.plot_col_basis_dict(B, col_idx=8, ax=axs[2])
No description has been provided for this image
In [34]:
rho_list = [0.1,0.2,0.5]

fig, axs = plt.subplots(3,3,figsize=(16,15))

for i in range(len(rho_list)):
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=50,
        type_cov='matern_3_2'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,0])
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,1], corr=True)
    axs[i,0].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Covariance')
    axs[i,1].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Correlation')
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=100,
        type_cov='matern_3_2'
    )
    bmlf.dataset_generator_toolbox.show_hat_matrix(B['B'], ax=axs[i,2])
    axs[i,2].set_title(r'$\rho = ' + f'{rho_list[i]:.2f}' + '$ Σ (no approximation)')
No description has been provided for this image
In [35]:
rho_list = [0.1,0.2,0.5]
fig, axs = plt.subplots(3,1,figsize=(13,15))

for i in range(len(rho_list)):
    B = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=50,
        type_cov='matern_3_2'
    )
    U = bmlf.build_matrix_B_from_gaussian_process_covariance(
        x,
        rho_list[i],
        num_singular_values=50,
        type_cov='matern_3_2',
        use_S=False
    )
    S = np.square( np.diag(U['B'].T@B['B']) )
    axs[i].plot(S, 'ko-')
    axs[i].set_title('Singular Values: ' + r'$\rho = ' + f'{rho_list[i]:.2f}' + '$')
    axs[i].set_ylim(bottom=0.0)
No description has been provided for this image
In [ ]: